package com.example.back.mapper;

import com.example.back.entity.Like;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;

@Mapper
public interface LikeMapper {
    //点赞
    @Insert("insert into `like`(postId,userId,time) values(#{postId},#{userId},#{time})")
    int addLike(Like like);
    //取消点赞（由用户决定，所以用userId删）
    @Delete("delete from `like` where postId=#{postId} and userId=#{userId}")
    int deleteLike(@Param("postId") Integer postId, @Param("userId") Integer userId);

    //只可改时间（感觉可以删，因为取消点赞再重写赞，又会生成新的时间7-19）
    @Update("update `like` set time=#{time} where postId=#{postId} and userId=#{userId}")
    int updateLike(Like like);
    @SelectProvider(type = LikeMapperProvider.class, method = "getQueryLikesSql")
    ArrayList<Like> queryLike(Like like);


    // 查询用户是否已点赞该帖子
    @Select("select * from `like` where postId=#{postId} and userId=#{userId}")
    Like getLikeByUserAndPost(@Param("postId") Integer postId, @Param("userId") Integer userId);

    // 统计帖子的点赞数
    @Select("select count(*) from `like` where postId=#{postId}")
    int countLikeByPostId(Integer postId);
    @Select("SELECT COUNT(*) FROM `like` WHERE postId IN (SELECT id FROM post WHERE userId =#{userId})")
    int countUserLikes(Integer userId);

}
